﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Management.Instrumentation;
using System.Management;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Collections;
namespace ConsoleApplication3
{
    public class cpu
    {
        int a, b;

        public int getCPUfrequency()
        {
            ManagementClass m = new ManagementClass("Win32_Processor");
            ManagementObjectCollection mn = m.GetInstances();
            ManagementObjectSearcher MySearcher = new ManagementObjectSearcher("SELECT * FROM Win32_Processor");
            foreach (ManagementObject MyObject in MySearcher.Get())
            {
                a = Convert.ToInt32(MyObject.Properties["CurrentClockSpeed"].Value);
                System.Console.WriteLine(MyObject["Name"].ToString());
                System.Console.WriteLine("CPU frequency：" + MyObject.Properties["CurrentClockSpeed"].Value.ToString());
            }
            return a;
        }
        public int getCPUcore()
        {
            ManagementClass m = new ManagementClass("Win32_Processor");
            ManagementObjectCollection mn = m.GetInstances();
            ManagementObjectSearcher MySearcher = new ManagementObjectSearcher("SELECT * FROM Win32_Processor");
            foreach (ManagementObject MyObject in MySearcher.Get())
            {
                PropertyDataCollection properties = MyObject.Properties;
                //System.Console.WriteLine("Number Of Cores:" + properties["NumberOfCores"].Value + "\r");
                b = Convert.ToInt32(properties["NumberOfCores"].Value);
                System.Console.WriteLine("Number Of Cores:" + b + "\r");
            }
            return b;
        }
        public void ThreadForCpuView()
        {
            float f = 0;
            PerformanceCounter[] counters = new PerformanceCounter[System.Environment.ProcessorCount];
            for (int i = 0; i < counters.Length; i++)
            {
                counters[i] = new PerformanceCounter("Processor", "% Processor Time", i.ToString());
            }

            while (true)
            {
                for (int i = 0; i < counters.Length; i++)
                {
                    f = counters[i].NextValue() + f;
                    Console.WriteLine("CPU-{0}: {1:f}%", i, f);
                }
                f = f / 2;
                Console.WriteLine("CPU:" + f);
                Console.WriteLine();
                System.Threading.Thread.Sleep(1000);
                f = 0;
            }
            /* // PerformanceCounter pcCpuLoad = (PerformanceCounter)obj;
                 PerformanceCounter pcCpuLoad = new PerformanceCounter("Process", "% Processor Time", "_Total");
                 pcCpuLoad.MachineName = ".";
                 pcCpuLoad.NextValue();

               //  Console.WriteLine("123");
            // float cpuLoad =  pcCpuLoad;

           //  Console.WriteLine("CPU load = " + pcCpuLoad + " %.");
            while (true)
             {
                 Thread.Sleep(100); // wait for 1 second 
                 float cpuLoad = pcCpuLoad.NextValue()/10;
                 Console.WriteLine("CPU load = " + cpuLoad + " %.");
                 pcCpuLoad.NextValue();
             }*/
        }
        public class Tsleep
        {
            int q, b, d;
            public void Psleep(int a, int o)
            {
                b = a;

                if (o == 1)
                    d = 300;
                else if (o == 2)
                    d = 75;
                else if (o == 3)
                    d = 1;
            }
            public void xsleep()
            {

                //System.Console.WriteLine(b * 0.001);
                for (; ; )
                {
                    q = 640000000;//b*1000000 *2/5;//(b *1024*1024*1024)/100;//17179870;
                    for (int i = 0; i < q; i++) ;
                    Thread.Sleep(d);
                }
            }
        }

        class Program
        {
            [DllImport("kernel32.dll")]
            static extern uint GetTickCount();

            //SetThreadAffinityMask Set thThread on the core you want dwThreadAffinityMask
            [DllImport("kernel32.dll")]
            static extern UIntPtr SetThreadAffinityMask(IntPtr hThread,
                                UIntPtr dwThreadAffinityMask);

            //get the  handler
            [DllImport("kernel32.dll")]
            static extern IntPtr GetCurrentThread();

            static void Main(string[] args)
            {

                int a = 0, b = 0, o;
                cpu x = new cpu();
                Tsleep Threadsleep = new Tsleep();
                //  x.ThreadForCpuView();
                a = x.getCPUfrequency();
                b = x.getCPUcore();
                Console.ReadLine();
                Thread[] threads = new Thread[8];
                Console.Write("Enter condition number(1-3):");
                string Condition = Console.ReadLine();
                o = Convert.ToInt32(Condition);

                if (o < 4 && o > 0)
                    Console.WriteLine(" Condition " + Condition);
                else
                    Console.WriteLine(" Enry error");

                o = Convert.ToInt32(Condition);
                //Console.WriteLine(o);
                if (b == 4)
                {
                    Threadsleep.Psleep(a, o);
                    SetThreadAffinityMask(GetCurrentThread(), new UIntPtr(0x0001));
                    Thread t0 = new Thread(new ThreadStart(Threadsleep.xsleep));

                    threads[0] = t0;

                    SetThreadAffinityMask(GetCurrentThread(), new UIntPtr(0x0002));
                    Thread t1 = new Thread(new ThreadStart(Threadsleep.xsleep));

                    threads[1] = t1;

                    SetThreadAffinityMask(GetCurrentThread(), new UIntPtr(0x0003));
                    Thread t2 = new Thread(new ThreadStart(Threadsleep.xsleep));

                    threads[2] = t2;

                    SetThreadAffinityMask(GetCurrentThread(), new UIntPtr(0x0004));
                    Thread t3 = new Thread(new ThreadStart(Threadsleep.xsleep));
                    // Thread t4 = new Thread(new ThreadStart( x.ThreadForCpuView));
                    threads[3] = t3;
                    //  threads[4] = t4;
                    for (int i = 0; i < 4; i++)
                    {
                        threads[i].Start();
                    }

                }
                else if (b == 2)
                {
                    Threadsleep.Psleep(a, o);
                    SetThreadAffinityMask(GetCurrentThread(), new UIntPtr(0x0001));
                    Thread t0 = new Thread(new ThreadStart(Threadsleep.xsleep));
                    threads[0] = t0;
                    SetThreadAffinityMask(GetCurrentThread(), new UIntPtr(0x0002));
                    Thread t1 = new Thread(new ThreadStart(Threadsleep.xsleep));
                    // Thread t2 = new Thread(new ThreadStart(x.ThreadForCpuView));
                    threads[1] = t1;
                    //  threads[2] = t2;
                    for (int i = 0; i < 3; i++)
                    {
                        threads[i].Start();
                    }
                }
                else if (b == 1)
                {
                    Threadsleep.Psleep(a, o);
                    //SetThreadAffinityMask(GetCurrentThread(), new UIntPtr(0x0001));
                    Thread t0 = new Thread(new ThreadStart(Threadsleep.xsleep));
                    threads[0] = t0;
                    //threads[0].Start();
                    Thread t1 = new Thread(new ThreadStart(x.ThreadForCpuView));
                    threads[1] = t1;
                    for (int i = 0; i < 2; i++)
                    {
                        threads[i].Start();
                    }
                    /*  System.Console.WriteLine(a * 0.001);
                      for (; ; )
                      {
                          q = (2 * a * 1000000) / 5;
                          //System.Console.WriteLine(b);
                          for (int i = 0; i < q; i++) ;
                          Thread.Sleep(5);
                      }*/
                }
            }
        }
    }
}


